package com.prs.crm.dao.oa.schedule.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.schedule.MyThingsDao;
import com.prs.crm.domain.oa.schedule.CommonSchedule;
import com.prs.crm.domain.oa.schedule.MyThings;

public class MyThingsDaoImpl extends BaseDaoImpl<MyThings> implements MyThingsDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<MyThings> getMythings() {
		return (List<MyThings>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(MyThings.class);
				return criteria.list();
			}
		});
	}

	@Override
	public void saveMyThings(MyThings myThings) {
		this.getHibernateTemplate().save(myThings);
	}

	@Override
	public MyThings getTime(final String time) {
		Criteria criteria=this.getSession().createCriteria(MyThings.class);
		criteria.add(Restrictions.eq("startTime", time));
		List result=criteria.list();
		if(result==null || result.size()==0){
			return null;
		}
		return (MyThings) result.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MyThings> findOtherThings(String name) {
		String hql="from MyThings as mt where mt.receivePersonNameId="+"'"+name+"'";
		Query query=this.getSession().createQuery(hql);
		return query.list();
	}
}
